#include <iostream>
#include <cstring>

using namespace std;

const int N = 510;

int n, m;
int f[N][N];
// f[i][j] : 表示{1,1}到{i,j}的最短路径
// f[i][j] = min(f[i - 1][j], f[i][j - 1]) + x
//                  上           左

int main()
{
    cin >> n >> m;

    // 初始化数组
    memset(f, 0x3f3f3f, sizeof f);
    f[0][1] = 0;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            int x; cin >> x;
            f[i][j] = min(f[i - 1][j], f[i][j - 1]) + x;
        }
    }

    cout << f[n][m] << endl;

    return 0;
}